const Koa = require("koa");
const Router = require("koa-router");
const mockList = require("./mock/index");

const app = new Koa();
const router = new Router();

async function getRes(fn, ctx) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(fn(ctx));
    }, 1000);
  });
}

mockList.forEach((item) => {
  const { method, path, response } = item;
  router[method](path, async (ctx) => {
    ctx.body = await getRes(response, ctx);
  });
});

app.use(router.routes());

const PORT = 3001;
app.listen(PORT, () => {
  console.log(`🚀 Mock服务器启动成功！`);
  console.log(`📍 服务地址: http://localhost:${PORT}`);
  console.log(`📋 可用接口:`);
  mockList.forEach((item) => {
    console.log(`   ${item.method.toUpperCase()} ${item.path}`);
  });
});
